<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;

use App\Model\AdminLogModel;
use BaseExt\Pagination;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\ORM\EntityManager;
use App\Entity\User;
use App\Model\LazyCommon\UserModel;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;

class BaseController4Admin extends BaseController {

    public function needToBeLogin()
    {
        $url = $this->generateUrl("AdminLogin");
        header("location:$url");
        die();
    }

    public function isAdmin()
    {
        $roles = $this->getSessionData("role");
        if (is_array($roles) && in_array("ROLE_ADMIN",$roles)) {
            return $this->getSessionData("id");
        } else {
            return false;
        }
    }

    public function needToBeAdmin()
    {
        if ($this->isAdmin()) {
            return $this->getSessionData("id");
        } else {
            $this->needToBeLogin();
        }
    }

    public function addLog($aid,$txt)
    {
        $request = Request::createFromGlobals();
        $AdminLogModel = new AdminLogModel($this->getDoctrine());
        $AdminLogModel->addLog($aid,$txt,$request);
    }

}
